package br.com.mynage.controller;

import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.util.HashMap;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import java.sql.Connection;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

@ManagedBean(name = "relatorio")
@RequestScoped
public class RelatorioMBean {
	public String formato;
	
	public RelatorioMBean() {
		
	}
	
	public String iniciarRelatorioTarefas() {
		return "/relatorios/form";	
	}
	
	public String gerarRelatorioTarefas() {
		
		     // Gerar relatório
	        Connection con = null;
	        try {
	        	
	            // Popular parâmetros do relatório
	            HashMap<String, Object> parametros = new HashMap<String, Object>();
	    
	            	try {
	            		// Preencher relatório
	            		con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mynage","postgres", "adm");
	            		JasperPrint prt = JasperFillManager.fillReport("C:/Users/sueltom/workspace/Mynage/relatorio/relatorio.jasper", parametros, con );
	            		// Exportar relatório de acordo com o formato escolhido
	    	            //String nomeArquivo = "relatorio."+formato;
	    	            
	    	            FileOutputStream fos = new FileOutputStream("C:/MynageFiles/relatorio.pdf");  
	    	            JasperExportManager.exportReportToPdfStream(prt, fos);
	    	            fos.flush();  
	    	            fos.close();
	    	            
	            	} catch (Exception e) {
	            		e.printStackTrace();
	            	}
	            
	            	return null;

	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        } finally {
	            
	        }
	
	}

	public String getFormato() {
		return formato;
	}

	public void setFormato(String formato) {
		this.formato = formato;
	}

}
